home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / Waste TCL r2 / WASTEEdit ƒ / CEditDoc.cp < prev    next >
Encoding:
Text File  |  1994-11-30  |  9.1 KB  |  381 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CEditDoc.c
  3.     
  4.     Document methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include "Global.h"
  11. #include "Commands.h"
  12. #include "CApplication.h"
  13. #include "CBartender.h"
  14. #include "CDataFile.h"
  15. #include "CDecorator.h"
  16. #include "CDesktop.h"
  17. #include "CError.h"
  18. #include "CPanorama.h"
  19. #include "CScrollPane.h"
  20. #include "TBUtilities.h"
  21. #include "CEditDoc.h"
  22. #include "CEditPane.h"
  23. #include "CWindow.h"
  24.  
  25. #define    WINDculture        500        /* Resource ID for WIND template */
  26.  
  27. extern    CApplication *gApplication;    /* The application */
  28. extern    CBartender    *gBartender;    /* The menu handling object */
  29. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  30. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  31. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  32. extern    OSType        gSignature;        /* The application's signature */
  33. extern    CError        *gError;        /* The error handling object */
  34.  
  35. /***
  36.  * IEditDoc
  37.  *
  38.  *    This is your document's initialization method.
  39.  *    If your document has its own instance variables, initialize
  40.  *    them here.
  41.  *    The least you need to do is invoke the default method.
  42.  *
  43.  ***/
  44.  
  45. void CEditDoc::IEditDoc(CApplication *aSupervisor, Boolean printable)
  46.  
  47. {
  48.     CDocument::IDocument(aSupervisor, printable);
  49. }
  50.  
  51.  
  52. /***
  53.  * NewFile
  54.  *
  55.  *    When the user chooses New from the File menu, the CreateDocument()
  56.  *    method in your Application class will send a newly created document
  57.  *    this message. This method needs to create a new window, ready to
  58.  *    work on a new document.
  59.  *
  60.  *    Since this method and the OpenFile() method share the code for creating
  61.  *    the window, you should use an auxiliary window-building method.
  62.  *
  63.  ***/
  64. void CEditDoc::NewFile(void)
  65.  
  66. {
  67.         /**
  68.          **    BuildWindow() is the method that
  69.          **    does the work of creating a window.
  70.          **    It's parameter should be the data that
  71.          **    you want to display in the window.
  72.          **    Since this is a new window, there's nothing
  73.          **    to display.
  74.          **
  75.          **/
  76.  
  77.     BuildWindow(NULL);
  78.  
  79.         /**
  80.          **    Send the window a Select() message to make
  81.          **    it the active window.
  82.          **/
  83.     
  84.     itsWindow->Select();
  85. }
  86.  
  87.  
  88. /***
  89.  * OpenFile
  90.  *
  91.  *    When the user chooses Open… from the File menu, the OpenDocument()
  92.  *    method in your Application class will let the user choose a file
  93.  *    and then send a newly created document this message. The information
  94.  *    about the file is in the SFReply record.
  95.  *
  96.  *    In this method, you need to open the file and display its contents
  97.  *    in a window. This method uses the auxiliary window-building method.
  98.  *
  99.  ***/
  100.  
  101. void CEditDoc::OpenFile(SFReply *macSFReply)
  102.  
  103. {
  104.     CDataFile    *theFile;
  105.     Handle        theData;
  106.     Str63        theName;
  107.     OSErr        theError;
  108.  
  109.         /**
  110.          ** Create a file and send it a SFSpecify()
  111.          **    message to set up the name, volume, and
  112.          **    directory.
  113.          **
  114.          **/
  115.  
  116.     theFile = new(CDataFile);
  117.  
  118.         /**
  119.          **    Be sure to set the instance variable
  120.          **    so other methods can use the file if they
  121.          **    need to. This is especially important if
  122.          **    you leave the file open in this method.
  123.          **    If you close the file after reading it, you
  124.          **    should be sure to set itsFile to NULL.
  125.          **
  126.          **/
  127.  
  128.     itsFile = theFile;
  129.  
  130.     theFile->IDataFile();
  131.     theFile->SFSpecify(macSFReply);
  132.     
  133.  
  134.         /**
  135.          **    Send the file an Open() message to
  136.          **    open it. You can use the ReadSome() or
  137.          **    ReadAll() methods to get the contents of the file.
  138.          **
  139.          **/
  140.  
  141.     theFile->Open(fsRdWrPerm);
  142.     
  143.     /*** no longer needed with WASTE
  144.     if (theFile->GetLength() > 10240L)
  145.     {
  146.         ParamText( "\pCan't open a file this big.", "\p", "\p", "\p");
  147.         PositionDialog('ALRT', 128);
  148.         InitCursor();
  149.         Alert(128, NULL);
  150.         
  151.         delete this;
  152.         return;
  153.     }
  154.     ***/
  155.  
  156.     theData = theFile->ReadAll();     /* ReadAll() creates the handle */
  157.  
  158.     BuildWindow(theData);
  159.  
  160.         /**
  161.          **    In your application, you'll probably store
  162.          **    the data in some form as an instance variable
  163.          **    in your document class. For this example, there's
  164.          **    no need to save it, so we'll get rid of it.
  165.          **
  166.          **/
  167.  
  168.     DisposHandle(theData);
  169.  
  170.         /**
  171.          **    In this implementation, we leave the file
  172.          **    open. You might want to close it after
  173.          **    you've read in all the data.
  174.          **
  175.          **/
  176.  
  177.     itsFile->GetName(theName);
  178.     itsWindow->SetTitle(theName);
  179.     itsWindow->Select();            /* Don't forget to make the window active */
  180. }
  181.  
  182.  
  183.  
  184. /***
  185.  * BuildWindow
  186.  *
  187.  *    This is the auxiliary window-building method that the
  188.  *    NewFile() and OpenFile() methods use to create a window.
  189.  *
  190.  *    In this implementation, the argument is the data to display.
  191.  *
  192.  ***/
  193.  
  194. void CEditDoc::BuildWindow (Handle theData)
  195.  
  196. {
  197.     CScrollPane        *theScrollPane;
  198.     CEditPane        *theMainPane;
  199.     Rect            margin;
  200.  
  201.         /**
  202.          **    First create the window and initialize
  203.          **    it. The first argument is the resource ID
  204.          **    of the window. The second argument specifies
  205.          **    whether the window is a floating window.
  206.          **    The third argument is the window's enclosure; it
  207.          **    should always be gDesktop. The last argument is
  208.          **    the window's supervisor in the Chain of Command;
  209.          **    it should always be the Document object.
  210.          **
  211.          **/
  212.  
  213.     itsWindow = new(CWindow);
  214.     itsWindow->IWindow(WINDculture, FALSE, gDesktop, this);
  215.     
  216.         /**
  217.          **    After you create the window, you can use the
  218.          **    SetSizeRect() message to set the window’s maximum
  219.          **    and minimum size. Be sure to set the max & min
  220.          **    BEFORE you send a PlaceNewWindow() message to the
  221.          **    decorator.
  222.          **
  223.          ** The default minimum is 100 by 100 pixels. The
  224.          **    default maximum is the bounds of GrayRgn() (The
  225.          **    entire display area on all screens.)
  226.          **
  227.          **/
  228.  
  229.     theScrollPane = new(CScrollPane);
  230.     
  231.         /**
  232.          **    You can initialize a scroll pane two ways:
  233.          **        1. You can specify all the values
  234.          **           right in your code, like this.
  235.          **        2. You can create a ScPn resource and
  236.          **           initialize the pane from the information
  237.          **           in the resource.
  238.          **
  239.          **/
  240.  
  241.     theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
  242.                                 sizELASTIC, sizELASTIC,
  243.                                 TRUE, TRUE, TRUE);
  244.  
  245.         /**
  246.          **    The FitToEnclFrame() method makes the
  247.          **    scroll pane be as large as its enclosure.
  248.          **    In this case, the enclosure is the window,
  249.          **    so the scroll pane will take up the entire
  250.          **    window.
  251.          **
  252.          **/
  253.  
  254.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  255.  
  256.  
  257.         /**
  258.          **    itsMainPane is the document's focus
  259.          **    of attention. Some of the standard
  260.          **    classes (particularly CPrinter) rely
  261.          **    on itsMainPane pointing to the main
  262.          **    pane of your window.
  263.          **
  264.          **    itsGopher specifies which object
  265.          **    should become the gopher. By default
  266.          **    the document becomes the gopher. It’s
  267.          **    likely that your main pane handles commands
  268.          **    so you’ll almost want to set itsGopher
  269.          **    to point to the same object as itsMainPane.
  270.          **
  271.          **    Note that the main pane is the
  272.          **    panorama in the scroll pane and not
  273.          **    the scroll pane itself.
  274.          **
  275.          **/
  276.  
  277.     theMainPane = new (CEditPane);
  278.  
  279.     itsMainPane = theMainPane;
  280.     itsGopher = theMainPane;
  281.  
  282.         /**
  283.          **    The IEditPane method automatically
  284.          **    fits the pane to the enclosure and
  285.          **    gives us a little margin.
  286.          **
  287.          **/
  288.  
  289.     theMainPane->IEditPane(theScrollPane, this);
  290.  
  291.         /**
  292.          **    Send the scroll pane an InstallPanorama()
  293.          **    to associate our pane with the scroll pane.
  294.          **
  295.          **/
  296.  
  297.     theScrollPane->InstallPanorama(theMainPane);
  298.  
  299.     if (theData)
  300.         theMainPane->SetTextHandle(theData);
  301.     
  302.         /**
  303.          **    The Decorator is a global object that takes care
  304.          **    of placing and sizing windows on the screen.
  305.          **    You don't have to use it.
  306.          **
  307.          **/
  308.  
  309.     gDecorator->PlaceNewWindow(itsWindow);
  310. }
  311.  
  312.  
  313.  
  314. /***
  315.  * DoSave
  316.  *
  317.  *    This method handles what happens when the user chooses Save from the
  318.  *    File menu. This method should return TRUE if the file save was successful.
  319.  *    If there is no file associated with the document, you should send a
  320.  *    DoSaveFileAs() message.
  321.  *
  322.  ***/
  323.  
  324. Boolean CEditDoc::DoSave(void)
  325.  
  326. {
  327.     Handle        theData;
  328.  
  329.     if (itsFile == NULL)
  330.         return(DoSaveFileAs());
  331.     else {
  332.         theData = ((CWASTEText *)itsMainPane)->GetTextHandle();
  333.         ((CDataFile *)itsFile)->WriteAll(theData);            
  334.         dirty = FALSE;                    /* Document is no longer dirty        */
  335.         gBartender->DisableCmd(cmdSave);
  336.         return(TRUE);                    /* Save was successful                */
  337.     }
  338. }
  339.  
  340.  
  341. /***
  342.  * DoSaveAs
  343.  *
  344.  *    This method handles what happens when the user chooses Save As… from
  345.  *    File menu. The default DoCommand() method for documents sends a DoSaveFileAs()
  346.  *    message which displays a standard put file dialog and sends this message.
  347.  *    The SFReply record contains all the information about the file you're about
  348.  *    to create.
  349.  *
  350.  ***/
  351.  
  352. Boolean CEditDoc::DoSaveAs(SFReply *macSFReply)
  353.  
  354. {
  355.         /**
  356.          **    If there's a file associated with this document
  357.          **    already, close it. The Dispose() method for files
  358.          **    sends a Close() message to the file before releasing
  359.          **    its memory.
  360.          **
  361.          **/
  362.          
  363.     TCLForgetObject(itsFile);
  364.  
  365.  
  366.         /**
  367.          **    Create a new file, and then save it normally.
  368.          **
  369.          **/
  370.  
  371.     itsFile = new(CDataFile);
  372.     ((CDataFile *)itsFile)->IDataFile();
  373.     itsFile->SFSpecify(macSFReply);
  374.     itsFile->CreateNew(gSignature, 'TEXT');
  375.     itsFile->Open(fsRdWrPerm);
  376.     
  377.     itsWindow->SetTitle(macSFReply->fName);
  378.  
  379.     return( DoSave() );
  380. }
  381.